<template>
  <div class="customer-form">
    <div class="page-header">
      <h2>{{ isEdit ? '编辑客户' : '添加客户' }}</h2>
    </div>
    
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="客户姓名" prop="name">
        <el-input v-model="form.name" placeholder="请输入客户姓名"></el-input>
      </el-form-item>
      
      <el-form-item label="电子邮箱" prop="email">
        <el-input v-model="form.email" placeholder="请输入电子邮箱"></el-input>
      </el-form-item>
      
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="form.phone" placeholder="请输入联系电话"></el-input>
      </el-form-item>
      
      <el-form-item label="联系地址" prop="address">
        <el-input type="textarea" v-model="form.address" placeholder="请输入联系地址" :rows="3"></el-input>
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" @click="submitForm">{{ isEdit ? '保存修改' : '添加客户' }}</el-button>
        <el-button @click="$router.push('/customers')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import customerApi from '@/api/customer'

export default {
  name: 'CustomerForm',
  
  data() {
    return {
      isEdit: false,
      form: {
        customerId: null,
        name: '',
        email: '',
        phone: '',
        address: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入客户姓名', trigger: 'blur' },
          { min: 2, max: 100, message: '长度在 2 到 100 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入电子邮箱', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
        ],
        phone: [
          { pattern: /^1[3-9]\d{9}$|^(\d{3,4}-)?\d{7,8}$/, message: '请输入正确的电话格式', trigger: 'blur' }
        ]
      }
    }
  },
  
  created() {
    // 判断是否为编辑模式
    const id = this.$route.params.id
    if (id) {
      this.isEdit = true
      this.fetchCustomer(id)
    }
  },
  
  methods: {
    // 获取客户详情
    async fetchCustomer(id) {
      try {
        const response = await customerApi.getCustomer(id)
        this.form = response.data
      } catch (error) {
        this.$message.error('获取客户信息失败')
      }
    },
    
    // 提交表单
    submitForm() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          try {
            let response
            if (this.isEdit) {
              response = await customerApi.updateCustomer(this.form.customerId, this.form)
            } else {
              response = await customerApi.addCustomer(this.form)
            }
            
            if (response.data.success) {
              this.$message.success(response.data.message)
              this.$router.push('/customers')
            } else {
              this.$message.error(response.data.message)
            }
          } catch (error) {
            this.$message.error(this.isEdit ? '更新客户失败' : '添加客户失败')
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
.customer-form {
  background-color: #fff;
  padding: 20px;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.page-header {
  margin-bottom: 20px;
}

.el-form {
  max-width: 500px;
}
</style> 